package com.itheima.ydd.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.Information;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.login.CredentialNotFoundException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_advpos")
@Slf4j
public class YddAdvposController {
    @Autowired
    private IYddAdvposService yddAdvposService;
    @Autowired
    private IYddAdvposService yddAdvimgService;

    /**
     * 分页查询
     * @param advposdesc
     * @param advposname
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData getList(@RequestParam String advposdesc,
                              @RequestParam String advposname,
                              @RequestParam(value = "page", required = false, defaultValue = "1") String page,
                              @RequestParam(value = "limit", required = false, defaultValue = "10") String limit){
        YddAdvpos yddAdvpos = new YddAdvpos();
        yddAdvpos.setAdvposdesc(advposdesc);
        yddAdvpos.setAdvposname(advposname);
        Information information = new Information();
        information = yddAdvposService.getList(yddAdvpos, page, limit);
        return ResultData.ok("成功",information);
    }

    /**
     * 根据id查广告
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(@RequestParam String id){
        log.info("查询的id是{}",id);
        YddAdvpos yddAdvpos = yddAdvposService.getById(id);
        return ResultData.ok("成功",yddAdvpos);
    }

    /**
     * 修改广告位
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody YddAdvpos yddAdvpos){
        log.info(yddAdvpos.toString());
        yddAdvposService.updateById(yddAdvpos);
        return R.success("Code",200);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R deleteById(@RequestParam String id){
        log.info("需要删除的id:{}",id);
        yddAdvposService.removeById(id);
        return R.success("Code",200);
    }

    /**
     * 新增广告位
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody YddAdvpos yddAdvpos){
        log.info("需要上传的广告是:{}",yddAdvpos.toString());
        yddAdvposService.save(yddAdvpos);
        return R.success("Code",200);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/all")
    public ResultData getAll (){
        List<YddAdvpos> list = yddAdvposService.list();
        return ResultData.ok("成功",list);
    }

}
